package com.bestcem.xm.award.service.bo.point;

import cn.hutool.core.date.DateUtil;
import com.bestcem.xm.award.constant.Constants;
import com.bestcem.xm.award.controller.vo.point.PointAwardVO;
import com.bestcem.xm.award.entity.pojo.AwardAwardDO;
import com.bestcem.xm.award.enums.AwardStatusEnum;
import com.bestcem.xm.award.enums.AwardTypeEnum;
import com.bestcem.xm.award.service.bo.award.AwardAwardBO;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * @author xa.zhang <xa.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/17 14:36
 * @desc 积分奖励创建修改参数，除了id，其他全部必填
 */
@Data
public class PointAwardBO {

    private String id;

    /**
     * 项目id
     */
    @JsonProperty("project_id")
    private String projectId;

    /**
     * 机构id
     */
    @JsonProperty("org_id")
    private String orgId;

    /**
     * 奖励名称
     */
    private String name;

    /**
     * 奖励基本配置
     */
    private Basic basic;

    /**
     * 限制规则
     */
    private Rule rule;

    /**
     * 积分奖励设置
     */
    @JsonProperty("band_point")
    private BandPoint bandPoint;

    /**
     * Optional. 奖励状态 {@link AwardStatusEnum}
     */
    private Integer status;

    /**
     * Required. 奖励类型 {@link AwardTypeEnum}
     */
    private Integer type;

    /**
     * 限制规则
     */
    @Data
    public static class Rule implements Serializable {

        /**
         * 可选,答题时间限制
         */
        private Integer time;

        /**
         * 可选 账户访问限制
         */
        private Integer account;
    }

    /**
     * 奖励基本配置项
     */
    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public static class Basic implements Serializable {
        /**
         * 抽奖开始时间
         */
        @JsonProperty("start_time")
        private String startTime;

        /**
         * 抽奖结束时间
         */
        @JsonProperty("stop_time")
        private String stopTime;

        /**
         * 红包页面展示的公司名称
         */
        @JsonProperty("display_name")
        private String displayName;

        /**
         * 红包页面展示的logo的id
         */
        @JsonProperty("display_logo_id")
        private String displayLogoId;

        /**
         * 红包页面展示的logo的的路径
         */
        @JsonProperty("display_logo_path")
        private String displayLogoPath;
    }

    /**
     * 品牌积分配置
     */
    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public static class BandPoint implements Serializable {

        /**
         * 单份奖励 1-1000
         */
        @JsonProperty("sin_point")
        @NotNull(message = "单份奖励必填")
        private Integer sinPoint;

        /**
         * 品牌积分id
         */
        @NotNull(message = "积分id必填")
        @JsonProperty("point_id")
        private String pointId;

        /**
         * 品牌积分序号
         */
        private String code;

        /**
         * 品牌积分名称
         */
        @NotBlank(message = "积分名称必填")
        @JsonProperty("point_name")
        private String pointName;

        /**
         * 来源备注
         */
        @NotBlank(message = "来源备注必填")
        private String remark;

        /**
         * 可领取的积分次数，可为空，为空表示不限制
         */
        private Integer total;

        /**
         * true表示有效期内达到领奖次数则提前结束奖励
         */
        @JsonProperty("stop_advance")
        private Boolean stopAdvance;

        /**
         * 领奖提示
         */
        @NotBlank(message = "领奖提示必填")
        private String tip;
    }


    public static PointAwardVO createAwardVO(AwardAwardBO param) {

        PointAwardVO award = new PointAwardVO();

        award.setId(param.getId());
        award.setName(param.getName());
        award.setOrgId(param.getOrgId());
        award.setProjectId(param.getProjectId());
        award.setStatus(param.getStatus());
        award.setType(param.getTType());

        PointAwardVO.Basic basic = new PointAwardVO.Basic();
        if (param.getBasic() != null) {

            basic.setStartTime(DateUtil.format(param.getBasic().getStartTime(), Constants.DATE_PATTERN));
            basic.setStopTime(DateUtil.format(param.getBasic().getStopTime(), Constants.DATE_PATTERN));
            basic.setDisplayLogoId(param.getBasic().getDisplayLogoId());
            basic.setDisplayName(param.getBasic().getDisplayName());
            basic.setDisplayLogoPath(param.getBasic().getDisplayLogoPath());

        }

        PointAwardVO.Rule rule = new PointAwardVO.Rule();
        rule.setAccount(param.getRule().getAccount());
        rule.setTime(param.getRule().getTime());

        PointAwardVO.BandPoint bandPoint = new PointAwardVO.BandPoint();
        if (param.getBandPoint() != null) {
            BeanUtils.copyProperties(param.getBandPoint(), bandPoint);
        }

        award.setBasic(basic);
        award.setRule(rule);
        award.setBandPoint(bandPoint);

        return award;
    }


    public static PointAwardBO createAwardVO(AwardAwardDO param) {

        PointAwardBO award = new PointAwardBO();

        award.setId(param.getId());
        award.setName(param.getName());
        award.setOrgId(param.getOrgId());
        award.setProjectId(param.getProjectId());
        award.setStatus(param.getStatus());
        award.setType(param.getTType());

        Basic basic = new Basic();
        if (param.getBasic() != null) {

            basic.setStartTime(DateUtil.format(com.bestcem.xm.common.core.uitls.DateUtil.getDate(param.getBasic().getStartTime()), Constants.DATE_PATTERN));
            basic.setStopTime(DateUtil.format(com.bestcem.xm.common.core.uitls.DateUtil.getDate(param.getBasic().getStopTime()), Constants.DATE_PATTERN));
            basic.setDisplayLogoId(param.getBasic().getDisplayLogoId());
            basic.setDisplayName(param.getBasic().getDisplayName());
            basic.setDisplayLogoPath(param.getBasic().getDisplayLogoPath());

        }

        Rule rule = new Rule();
        rule.setAccount(param.getRule().getAccount());
        rule.setTime(param.getRule().getTime());

        BandPoint bandPoint = new BandPoint();
        if (param.getBandPoint() != null) {
            BeanUtils.copyProperties(param.getBandPoint(), bandPoint);
        }

        award.setBasic(basic);
        award.setRule(rule);
        award.setBandPoint(bandPoint);

        return award;
    }

}
